using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region Department

	/// <summary>
	/// Department object for NHibernate mapped table 'Department'.
	/// </summary>
	[Serializable]
	public class Department : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected string _departmentCode;
		protected string _description;
		protected DicItem _departmentType;
		protected Employee _departmentHead;
		protected IList _departmentNameApplications;
		protected IList _cATBranchUserRoles;
		protected IList _departmentEmployees;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public Department() { }

		public Department( string departmentCode, string description, DicItem departmentType, Employee departmentHead )
		{
			this._departmentCode = departmentCode;
			this._description = description;
			this._departmentType = departmentType;
			this._departmentHead = departmentHead;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public string DepartmentCode
		{
			get { return _departmentCode; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for DepartmentCode", value, value.ToString());
				_departmentCode = value;
			}
		}

		public string Description
		{
			get { return _description; }
			set
			{
				if ( value != null && value.Length > 500)
					throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());
				_description = value;
			}
		}

		public DicItem DepartmentType
		{
			get { return _departmentType; }
			set { _departmentType = value; }
		}

		public Employee DepartmentHead
		{
			get { return _departmentHead; }
			set { _departmentHead = value; }
		}

		public IList DepartmentNameApplications
		{
			get
			{
				if (_departmentNameApplications==null)
				{
					_departmentNameApplications = new ArrayList();
				}
				return _departmentNameApplications;
			}
			set { _departmentNameApplications = value; }
		}

		public IList CATBranchUserRoles
		{
			get
			{
				if (_cATBranchUserRoles==null)
				{
					_cATBranchUserRoles = new ArrayList();
				}
				return _cATBranchUserRoles;
			}
			set { _cATBranchUserRoles = value; }
		}

		public IList DepartmentEmployees
		{
			get
			{
				if (_departmentEmployees==null)
				{
					_departmentEmployees = new ArrayList();
				}
				return _departmentEmployees;
			}
			set { _departmentEmployees = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is Department))
				throw new InvalidCastException("This object is not of type Department");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((Department)obj).Id);
					break;
				case "DepartmentCode":
					relativeValue = this.DepartmentCode.CompareTo(((Department)obj).DepartmentCode);
					break;
				case "Description":
						relativeValue = ( this.Description != null ) ? this.Description.CompareTo(((Department)obj).Description) : -1;
					break;
                default:
                    goto case "Id";
			}
            if (Department.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
